package com.schedualnew.mq.util;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DateUtils {
    private static Logger logger = LoggerFactory.getLogger(DateUtils.class);

    public static final String TYPE_DATE = "date";
    public static final String PATTERN_DATE = "yyyy-MM-dd";
    public static final String PATTERN_TIME = "HH:mm:ss";

    public static final String PARRTEN_DATE_TIME = PATTERN_DATE + " " + PATTERN_TIME;


    public static String last(String dateString) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = (new SimpleDateFormat("yy-MM-dd")).parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(5);
        c.set(5, day - 1);

        String dayBefore = (new SimpleDateFormat("yyyy-MM-dd")).format(c
                .getTime());
        return dayBefore;
    }


    public static String getCurrentBeforeTime() {
        Calendar c = Calendar.getInstance();


        c.set(11, c.get(11) - 1);
        String timeBefore = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(c
                .getTime());
        return timeBefore;
    }


    public static String format(String dateString, String srcFormapt, String desFormat) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = (new SimpleDateFormat(srcFormapt)).parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);

        String d = (new SimpleDateFormat(desFormat)).format(c.getTime());
        return d;
    }

    public static String format(Date date, String fmt) {
        DateFormat formatter = new SimpleDateFormat(fmt);
        return formatter.format(date);
    }

    public static String format(String date, String fmt) {
        DateFormat formatter = new SimpleDateFormat(fmt);
        return formatter.format(date);
    }


    public static String next(String dateString) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = (new SimpleDateFormat("yy-MM-dd")).parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(5);
        c.set(5, day + 1);

        String dayAfter = (new SimpleDateFormat("yyyy-MM-dd")).format(c.getTime());
        return dayAfter;
    }

    public static int getWeekDay(String strDay) {
        Date day = dateAdd(strDay, -1);
        Calendar strDate = Calendar.getInstance();
        strDate.setTime(day);
        int meStrDate = strDate.get(7);
        return meStrDate;
    }

    public static int getWeekDay(Date date) {
        Date day = dateAdd(format(date, TYPE_DATE), -1);
        Calendar strDate = Calendar.getInstance();
        strDate.setTime(day);
        int meStrDate = strDate.get(7);
        return meStrDate;
    }


    public static int getIntervalDays(String start, String end) {
        Calendar sc = Calendar.getInstance();

        Calendar ec = Calendar.getInstance();
        Date sdate = null;
        Date edate = null;
        try {
            sdate = (new SimpleDateFormat("yy-MM-dd")).parse(start);
            edate = (new SimpleDateFormat("yy-MM-dd")).parse(end);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sc.setTime(sdate);
        ec.setTime(edate);
        return Math.abs((int) ((ec.getTimeInMillis() - sc.getTimeInMillis()) / 86400000L));
    }


    public static int getIntervalDays(String date) {
        Calendar sc = Calendar.getInstance();

        Calendar ec = Calendar.getInstance();
        Date sdate = new Date();
        Date edate = null;
        try {
            sdate = (new SimpleDateFormat("yy-MM-dd")).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sc.setTime(sdate);
        ec.setTime(edate);
        return Math.abs((int) ((ec.getTimeInMillis() - sc.getTimeInMillis()) / 86400000L));
    }


    public static int getBirthDays(String dateString) {
        Calendar now = Calendar.getInstance();

        Calendar post = Calendar.getInstance();
        Date date = null;
        try {
            date = (new SimpleDateFormat("yy-MM-dd")).parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        post.setTime(date);
        return (int) ((now.getTimeInMillis() - post.getTimeInMillis()) / 86400000L);
    }


    public static boolean isOverdue(String dateString) {
        String today = (new SimpleDateFormat("yyyy-MM-dd")).format(Long.valueOf((new Date()).getTime()));
        return (today.compareTo(dateString) > 0);
    }


    public static String getCurrentDate() {
        return (new SimpleDateFormat("yyyy-MM-dd")).format(Long.valueOf((new Date()).getTime()));
    }


    public static String getCurrentTime() {
        return (new SimpleDateFormat("HH:mm:ss")).format(Long.valueOf((new Date()).getTime()));
    }

    public static String getCurrentDateTime() {
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(Long.valueOf((new Date()).getTime()));
    }


    public static String getAge(String dateString) {
        Calendar c = Calendar.getInstance();
        Calendar b = Calendar.getInstance();
        Date date = null;
        try {
            date = (new SimpleDateFormat("yyyy-MM-dd")).parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

        b.setTime(date);
        int y = c.get(1);
        int m = c.get(2);
        int d = c.get(5);

        int by = b.get(1);
        int bm = b.get(2);
        int bd = b.get(5);

        int age = y - by;
        int month = m - bm;
        int day = d - bd;
        if (day < 0) {
            month--;
            day += 30;
        }

        if (month < 0) {
            age--;
            month += 12;
        }

        if (age == 0 && month == 0) {
            day = getBirthDays(dateString);
        }

        String s = "";
        if (age > 0) {
            s = age + "岁";
        }
        if (month > 0 && age < 6) {
            s = s + month + "月";
        }
        if (day > 0 && age == 0) {
            s = s + day + "天";
        }

        return s;
    }


    public static Map<String, String> stringToMap(String str) {
        String[] arr = str.split(",");
        Map<String, String> map = new HashMap<>();
        for (String a : arr) {
            String[] array = a.split("=");
            map.put(array[0], array[1]);
        }
        return map;
    }


    public static String addMinutes(String date, int m) {
        Date d = null;
        Calendar c = Calendar.getInstance();
        try {
            d = (new SimpleDateFormat("yy-MM-dd HH:mm:ss")).parse(date);
        } catch (ParseException e) {
        }
        c.setTime(d);
        c.add(Calendar.MINUTE, m);
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(c.getTime());
    }

    public static Date addMinutes(Date date, int m) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, m);
        return c.getTime();
    }


    public static String addDays(String date, int m) {
        Date d = null;
        Calendar c = Calendar.getInstance();
        try {
            d = (new SimpleDateFormat("yy-MM-dd HH:mm:ss")).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(d);
        c.add(5, m);
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(c.getTime());
    }

    public static boolean checkReachTime(String datetime) {
        boolean result = true;
        if (StringUtils.isNotBlank(datetime)) {
            Date d = null;
            try {
                d = (new SimpleDateFormat("yy-MM-dd HH:mm:ss")).parse(datetime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date now = new Date();
            if (!now.before(d)) {
                result = false;
            }
        }
        return result;
    }

    /**
     * 获取时间分钟差值
     *
     * @param date
     * @param date2
     * @return
     */
    public static int diffMin(Date date, Date date2) {
        return (int) ((getMillis(date) - getMillis(date2)) / 60000L);
    }

    /**
     * 获取毫秒数
     *
     * @param date
     * @return
     */
    public static long getMillis(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }


    public static String getDateTime() {
        return DateToString(new Date());
    }

    public static String DateToString(final Date date) {
        if (date == null || date.equals("")) {
            return null;
        }
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String result = null;
        result = format.format(date);
        return result;
    }


    public static Date addSecond(Date date, int second) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.SECOND, second);
        return new Date(cal.getTime().getTime());
    }

    public static String addSecond(String time, int second) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.SECOND, second);
        return format.format(cal.getTime());
    }

    public static String addSeconds(String dateString, int s) {
        try {
            Date date = new SimpleDateFormat("HH:mm:ss").parse(dateString);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.SECOND, s);
            return new SimpleDateFormat("HH:mm:ss").format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getCurrDate() {
        return format(new Date(), "yyyy-MM-dd");
    }


    public static Date parse(String str, String parten) {
        DateFormat formatter = new SimpleDateFormat(parten);
        try {
            return formatter.parse(str);
        } catch (ParseException e) {
            //throw new RuntimeException(e);
            return null;
        }
    }


    public static java.sql.Date dateAdd(java.sql.Date date, int to) {
        Calendar strDate = Calendar.getInstance();
        strDate.setTime(date);
        strDate.add(Calendar.DATE, to);
        return new java.sql.Date(strDate.getTime().getTime());
    }

    public static java.sql.Date dateAdd(Date date, int to) {
        Calendar strDate = Calendar.getInstance();
        strDate.setTime(date);
        strDate.add(Calendar.DATE, to);
        return new java.sql.Date(strDate.getTime().getTime());
    }

    public static java.sql.Date dateAdd(int to) {
        return dateAdd(new Date(), to);
    }

    public static Date dateAdd(String date, int to) {
        Date d = null;
        try {
            d = java.sql.Date.valueOf(date);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            d = new Date();
        }
        Calendar strDate = Calendar.getInstance();
        strDate.setTime(d);
        strDate.add(Calendar.DATE, to);
        return strDate.getTime();
    }
}
